package com.bw.services.device.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bw.services.R
import com.bw.services.device.model.DeviceEntity
import androidx.core.content.ContextCompat

class DeviceAdapter(private val dataList:DeviceEntity):RecyclerView.Adapter<DeviceAdapter.MyViewHolder>() {
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_device_name: TextView = itemView.findViewById(R.id.tv_device_name)
        val tv_status: TextView = itemView.findViewById(R.id.tv_status)
        val tv_device_type: TextView = itemView.findViewById(R.id.tv_device_type)
        val tv_device_location: TextView = itemView.findViewById(R.id.tv_device_location)
        val tv_device_fault_rate: TextView = itemView.findViewById(R.id.tv_device_fault_rate)
    }
    // 创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_device, parent, false)
        return MyViewHolder(view)
    }

    // 绑定数据
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = dataList[position]
        holder.tv_device_name.text = "项目名称/设备名称"+item.deviceName
        holder.tv_device_type.text = "设备类型："+item.deviceType
        holder.tv_device_location.text = "安装地点："+item.installationLocation
        holder.tv_device_fault_rate.text = "故障率："+item.failureRate*100+"%"
        holder.tv_status.text = item.status
        // 新增：根据状态设置背景和文字颜色
        if (item.status == "正常") {
            holder.tv_status.background = ContextCompat.getDrawable(holder.itemView.context, R.drawable.bg_status_fault2)
            holder.tv_status.setTextColor(ContextCompat.getColor(holder.itemView.context, android.R.color.holo_green_dark))
        } else if (item.status == "故障") {
            holder.tv_status.background = ContextCompat.getDrawable(holder.itemView.context, R.drawable.bg_status_fault)
            holder.tv_status.setTextColor(ContextCompat.getColor(holder.itemView.context, android.R.color.holo_orange_dark))
        }
    }

    // 获取数据数量
    override fun getItemCount() = dataList.size
}